import { Controller, Post, Body, Get, Req, UseGuards, Param, Delete } from '@nestjs/common';
import { AdminService } from './admin.service';
import { AdminLoginDto } from './dto/admin-login.dto';
import { AdminRegisterDto } from './dto/admin-register.dto';
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { AdminJwtAuthGuard } from '../../common/admin-jwt-auth.guard';
import { UserService } from '../user/user.service';
import { ParseIntPipe } from '@nestjs/common';
import { ProductService } from '../product/product.service';
import { OrderService } from '../order/order.service';

@ApiTags('后台-管理员')
@Controller('admin')
export class AdminController {
  constructor(
    private readonly adminService: AdminService,
    private readonly userService: UserService,
  ) {}

  @Post('register')
  @ApiOperation({ summary: '管理员注册' })
  async register(@Body() dto: AdminRegisterDto) {
    return this.adminService.register(dto);
  }

  @Post('login')
  @ApiOperation({ summary: '管理员登录' })
  async login(@Body() dto: AdminLoginDto) {
    return this.adminService.login(dto);
  }

  @Get('me')
  @ApiBearerAuth()
  @UseGuards(AdminJwtAuthGuard)
  @ApiOperation({ summary: '获取当前管理员信息' })
  async getProfile(@Req() req) {
    // 这里仅做示例，实际应通过JWT获取id
    return this.adminService.findById(req.user?.sub);
  }

  // 用户管理后台接口
  @Get('users')
  @ApiBearerAuth()
  @UseGuards(AdminJwtAuthGuard)
  @ApiOperation({ summary: '用户列表' })
  async getUserList() {
    return this.userService.findAll();
  }

  @Get('users/:id')
  @ApiBearerAuth()
  @UseGuards(AdminJwtAuthGuard)
  @ApiOperation({ summary: '用户详情' })
  async getUserDetail(@Param('id', ParseIntPipe) id: number) {
    return this.userService.findById(id);
  }

  @Delete('users/:id')
  @ApiBearerAuth()
  @UseGuards(AdminJwtAuthGuard)
  @ApiOperation({ summary: '删除用户' })
  async deleteUser(@Param('id', ParseIntPipe) id: number) {
    return this.userService.remove(id);
  }

  // 角色权限校验示例（可扩展为装饰器/守卫）
  // @UseGuards(AdminRoleGuard)
  // @ApiOperation({ summary: '仅超级管理员可访问' })
  // @Get('super-only')
  // superOnlyEndpoint() {
  //   return '只有超级管理员可以访问';
  // }
}
